﻿#include <windows.h>
#include <string>
#include <stack>
#include <cmath>

// 全局变量
HWND hEdit; // 显示框句柄
std::wstring displayText = L"0"; // 显示文本
double result = 0; // 计算结果
std::wstring currentOperation = L""; // 当前操作
bool newInput = true; // 是否开始新输入

// 函数声明
void AddButton(HWND hWnd, LPCWSTR text, int x, int y, int width, int height, int id);
void OnButtonClick(int buttonId);
double Calculate(double a, double b, const std::wstring& op);


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        // 创建显示框
        hEdit = CreateWindowW(L"EDIT", L"0",
            WS_VISIBLE | WS_CHILD | ES_RIGHT | ES_READONLY,
            10, 10, 230, 30, hWnd, NULL, NULL, NULL);

        // 创建按钮
        AddButton(hWnd, L"C", 10, 50, 50, 30, 1);
        AddButton(hWnd, L"±", 70, 50, 50, 30, 2);
        AddButton(hWnd, L"%", 130, 50, 50, 30, 3);
        AddButton(hWnd, L"÷", 190, 50, 50, 30, 4);

        AddButton(hWnd, L"7", 10, 90, 50, 30, 5);
        AddButton(hWnd, L"8", 70, 90, 50, 30, 6);
        AddButton(hWnd, L"9", 130, 90, 50, 30, 7);
        AddButton(hWnd, L"×", 190, 90, 50, 30, 8);

        AddButton(hWnd, L"4", 10, 130, 50, 30, 9);
        AddButton(hWnd, L"5", 70, 130, 50, 30, 10);
        AddButton(hWnd, L"6", 130, 130, 50, 30, 11);
        AddButton(hWnd, L"-", 190, 130, 50, 30, 12);

        AddButton(hWnd, L"1", 10, 170, 50, 30, 13);
        AddButton(hWnd, L"2", 70, 170, 50, 30, 14);
        AddButton(hWnd, L"3", 130, 170, 50, 30, 15);
        AddButton(hWnd, L"+", 190, 170, 50, 30, 16);

        AddButton(hWnd, L"0", 10, 210, 110, 30, 17);
        AddButton(hWnd, L".", 130, 210, 50, 30, 18);
        AddButton(hWnd, L"=", 190, 210, 50, 30, 19);
    }
    break;
    case WM_COMMAND:
    {
        int buttonId = LOWORD(wParam);
        OnButtonClick(buttonId);
    }
    break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


void AddButton(HWND hWnd, LPCWSTR text, int x, int y, int width, int height, int id)
{
    CreateWindowW(L"BUTTON", text,
        WS_VISIBLE | WS_CHILD,
        x, y, width, height, hWnd, (HMENU)id, NULL, NULL);
}



void OnButtonClick(int buttonId)
{
    switch (buttonId)
    {
    case 1: // C - 清除
        displayText = L"0";
        result = 0;
        currentOperation = L"";
        newInput = true;
        break;
    case 2: // ± - 正负号
        if (displayText != L"0")
        {
            if (displayText[0] == L'-')
                displayText = displayText.substr(1);
            else
                displayText = L"-" + displayText;
        }
        break;
    case 3: // % - 百分比
    {
        double value = std::stod(displayText);
        value /= 100;
        displayText = std::to_wstring(value);
        // 移除多余的小数位
        size_t pos = displayText.find(L'.');
        if (pos != std::wstring::npos)
        {
            // 保留最多6位小数
            if (displayText.length() > pos + 7)
                displayText = displayText.substr(0, pos + 7);
        }
    }
    break;
    case 4: // ÷
    case 8: // ×
    case 12: // -
    case 16: // +
        if (!currentOperation.empty())
        {
            // 如果已有操作，先计算之前的结果
            double currentValue = std::stod(displayText);
            result = Calculate(result, currentValue, currentOperation);
            displayText = std::to_wstring(result);
        }
        else
        {
            result = std::stod(displayText);
        }

        switch (buttonId)
        {
        case 4: currentOperation = L"÷"; break;
        case 8: currentOperation = L"×"; break;
        case 12: currentOperation = L"-"; break;
        case 16: currentOperation = L"+"; break;
        }
        newInput = true;
        break;
    case 5: // 7
    case 6: // 8
    case 7: // 9
    case 9: // 4
    case 10: // 5
    case 11: // 6
    case 13: // 1
    case 14: // 2
    case 15: // 3
    case 17: // 0
        if (newInput || displayText == L"0")
        {
            displayText = std::to_wstring(buttonId - (buttonId >= 17 ? 16 : 4));
            newInput = false;
        }
        else
        {
            displayText += std::to_wstring(buttonId - (buttonId >= 17 ? 16 : 4));
        }
        break;
    case 18: // . - 小数点
        if (newInput)
        {
            displayText = L"0.";
            newInput = false;
        }
        else if (displayText.find(L'.') == std::wstring::npos)
        {
            displayText += L".";
        }
        break;
    case 19: // = - 等于
        if (!currentOperation.empty())
        {
            double currentValue = std::stod(displayText);
            result = Calculate(result, currentValue, currentOperation);
            displayText = std::to_wstring(result);
            currentOperation = L"";
            newInput = true;
        }
        break;
    }

    // 更新显示
    SetWindowTextW(hEdit, displayText.c_str());
}



double Calculate(double a, double b, const std::wstring& op)
{
    if (op == L"+") return a + b;
    if (op == L"-") return a - b;
    if (op == L"×") return a * b;
    if (op == L"÷") return a / b;
    return b; // 默认返回第二个操作数
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 注册窗口类
    WNDCLASSW wc = { 0 };
    wc.lpszClassName = L"CalculatorApp";
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);

    // 创建窗口
    HWND hWnd = CreateWindowW(wc.lpszClassName, L"计算器",
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
        100, 100, 260, 290, 0, 0, hInstance, 0);

    if (!hWnd) return 0;

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}